# -*- coding: utf-8 -*-
# @Time    : 2018/4/9 18:17
# @Author  : Soft
# @File    : Service.py
# @Software: PyCharm
# #Desc    : 
from module.models import Location, db

class LocalService:
    def __init__(self):
        pass

    @staticmethod
    @db.atomic()
    def add(**local):
        return Location.insert(**local).execute()

    @staticmethod
    @db.atomic()
    def remove(lid):
        if lid and lid > 0:
            return Location.delete_by_id(lid)
        return None

    @staticmethod
    @db.atomic()
    def change(lid, **local):
        return Location.update(**local).where(Location.id == lid).execute()

    @staticmethod
    def getByName(name):
        l = Location.select().where(Location.location_name == name)
        if l and len(l) > 0:
            return l[0].to_dict()
        return None

    @staticmethod
    def getOne(lid):
        return Location.find_by_id(lid)

    @staticmethod
    def getAll():
        ls = []
        for l in Location.select().order_by(Location.id.desc()):
            ls.append(l.to_dict())
        return ls
